//
//  ViewController.m
//  07-nsoperationqueue实现线程间通信
//
//  Created by J on 16/2/24.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  
    NSOperationQueue* queue = [[NSOperationQueue alloc]init];
  __block  UIImage*  image1 = nil;
    
    NSBlockOperation* downLoad1 = [NSBlockOperation blockOperationWithBlock:^{
        
        // 图片的网络路径
        NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
        
        // 加载图片
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        // 生成图片
        image1 = [UIImage imageWithData:data];

        
    }];

    

    
    
    __block UIImage *image2 = nil;

    NSBlockOperation* downLoad2 = [NSBlockOperation blockOperationWithBlock:^{
        // 图片的网络路径
        NSURL *url = [NSURL URLWithString:@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];
        
        // 加载图片
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        // 生成图片
        image2 = [UIImage imageWithData:data];

        
    }];
    

    
    //合成操作
    NSBlockOperation* combine = [NSBlockOperation blockOperationWithBlock:^{
        
        //回到主线程
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            
            
            // 开启新的图形上下文
            UIGraphicsBeginImageContext(CGSizeMake(100, 100));
            
            // 绘制图片
            [image1 drawInRect:CGRectMake(0, 0, 50, 100)];
            image1 = nil;
            
            [image2 drawInRect:CGRectMake(50, 0, 50, 100)];
            image2 = nil;
            
            // 取得上下文中的图片
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            
            // 结束上下文
            UIGraphicsEndImageContext();
            
            self.imageView.image = image;

    
        }];
        
    }];
    [combine addDependency:downLoad1];
    [combine addDependency:downLoad2];
    
    [queue addOperation:downLoad1];
    [queue addOperation:downLoad2];
    [queue addOperation:combine];
    
    
}


- (void)test{//线程之间通讯
    
    NSOperationQueue* queue = [[NSOperationQueue alloc]init];
    [queue addOperationWithBlock:^{
        //下载图片
        NSURL* url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
        
        
        //加载图片
        NSData* data = [NSData dataWithContentsOfURL:url];
        //生成图片
        UIImage* image = [UIImage imageWithData:data];
        
        
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            
            self.imageView.image = image;
        }];
        
        
    }];
    
   
    
    
}

@end
